
#include "UIDisplayView.h"
#include <SDL2/SDL_opengl.h>

#include <ALittleClient/LogSystem/LogSystem.h>

namespace ALittle
{

UIDisplayView::UIDisplayView()
: m_visible(true), m_x(0), m_y(0), m_width(0), m_height(0)
{ }

UIDisplayView::~UIDisplayView()
{ }

void UIDisplayView::SetVisible(bool visible)
{
	m_visible = visible;
}

void UIDisplayView::SetX(float x)
{
	m_x = x;
}

void UIDisplayView::SetY(float y)
{
	m_y = y;
}

void UIDisplayView::SetWidth(float width)
{
	m_width = width;
}

void UIDisplayView::SetHeight(float height)
{
	m_height = height;
}

void UIDisplayView::Render()
{
	if (!m_visible) return;
	if (m_display_list.size() == 0) return;
	if (m_width <= 0 || m_height <= 0) return;

	glPushMatrix();
	glTranslatef(m_x, m_y, 0);
	DisplayList::iterator it, end = m_display_list.end();
	for (it = m_display_list.begin(); it != end; ++it)
	{
		glEnable(GL_SCISSOR_TEST);
		glScissor((unsigned int)m_x, (unsigned int)(m_y + m_width), (unsigned int)m_width, (unsigned int)m_height);
		(*it)->Render();
		glDisable(GL_SCISSOR_TEST);
	}
	glPopMatrix();
}

void UIDisplayView::AddChild(UIDisplayObject* object)
{
	if (object == NULL)
	{
		ALITTLE_ERROR("object is null!");
		return;
	}
	m_display_list.push_back(object);
}

void UIDisplayView::RemoveChild(UIDisplayObject* object)
{
	if (object == NULL)
	{
		ALITTLE_ERROR("object is null!");
		return;
	}

	m_display_list.remove(object);
}

void UIDisplayView::RemoveAll()
{
	m_display_list.resize(0);
}

} // ALittle
